package com.coffeeloversupportlibrary.coffeelover.domain.article;

import com.coffeeloversupportlibrary.coffeelover.ApiService;
import com.coffeeloversupportlibrary.coffeelover.callback.ModelCallBack;
import com.coffeeloversupportlibrary.coffeelover.domain.article.entity.Article;
import com.coffeeloversupportlibrary.coffeelover.domain.article.entity.OriginData;
import com.coffeeloversupportlibrary.coffeelover.networktest.entity.Beans;
import com.coffeeloversupportlibrary.core.base.BaseModel;
import com.coffeeloversupportlibrary.core.data.IDataRepository;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author puyv
 * @createTime 2021/11/6 20:10
 * @projectName CoffeeLoverSupportLibrary
 * @className ArticleModel.java
 * @description TODO
 */
public class ArticleModel extends BaseModel {
    private List<Article> articles;

    public ArticleModel(IDataRepository dataRepository) {
        super(dataRepository);
    }

    public void getAllArticle(ModelCallBack<List<Article>,String> callBack){
        getRetrofitService(ApiService.class).requestArticleList().enqueue(new Callback<OriginData>() {
            @Override
            public void onResponse(Call<OriginData> call, Response<OriginData> response) {
                  if(response.body().isSuccessful()){
                      articles=new ArrayList<>();
                      for(OriginData.DataDTO.ArticleListDTO dto:response.body().getResult()){
                          Article article=new Article(dto.getArticleId(),dto.getArticleTitle(), dto.getArticleBanner(), dto.getArticleType(), dto.getCreateTime(), dto.getUpdateTime(), dto.getDeleted());
                          articles.add(article);
                      }
                      callBack.onSuccess(articles);
                  }else{
                      //  非成功网络请求判定，返回相关信息
                      callBack.Failure(response.body().getMessage());
                  }
            }

            @Override
            public void onFailure(Call<OriginData> call, Throwable t) {
                     callBack.Failure("onFailure!");
            }
        });
    }

}
